/**
 * 初始化部门详情对话框
 */
var ReservationsInfoDlg = {
    reservationsInfoData: {},
    zTreeInstance: null,
    validateFields: {
        customerName: {
            validators: {
                notEmpty: {
                    message: '客户姓名不能为空'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '客户姓名长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        customerPhone: {
            validators: {
                notEmpty: {
                    message: '电话不能为空'
                },
                regexp: {
                    /* 只需加此键值对，包含正则表达式，和提示 */
                    regexp: /^\d{5,13}$/,
                    message: '电话为5到13位的数字，请重新输入...'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '电话长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        archiving: {
            validators: {
                notEmpty: {
                    message: '建档人不能为空'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '建档人长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        qqNumber: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 25,
                    message: 'qq号长度不能超过25，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        wechatNumber: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 45,
                    message: '微信号长度不能超过45，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        otherContact: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 100,
                    message: '其他联系方式长度不能超过100，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        follower: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 25,
                    message: '跟进人长度不能超过25，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        obstaclePoint: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 30000,
                    message: '障碍点长度不能超过3万，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        quarters: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 100,
                    message: '小区长度不能超过100，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        quarters: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 100,
                    message: '小区长度不能超过100，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        customerAge: {
            validators: {
                regexp: {
                    /* 只需加此键值对，包含正则表达式，和提示 */
                    regexp: /^([0-9]\d{0,2}|1000)$/,
                    message: '年龄为 0 ~ 1000 间整数，请重新输入...'
                },
            }
        }
    }
};

/**
 * 清除数据
 */
ReservationsInfoDlg.clearData = function () {
    this.reservationsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReservationsInfoDlg.set = function (key, val) {
    this.reservationsInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReservationsInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ReservationsInfoDlg.close = function () {
    // parent.layer.close(window.parent.Reservations.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

/**
 * 编辑客户详情
 */
ReservationsInfoDlg.getEdit = function () {
    $(".modify1").attr("readOnly", false);
    $(".modify1").css("border", "1px solid #999999");
    $(".select_styles1").removeAttr("disabled");
    $(".select_styles1").css("border", "1px solid #999999");
}

ReservationsInfoDlg.getEdits = function () {
    $("#remarks").attr("readOnly", false);
    $("#content").attr("readOnly", false);
    $(".modify").attr("readOnly", false);
    $(".modify").css("border", "1px solid #999999");
    $(".select_styles").removeAttr("disabled");
    $(".select_styles").css("border", "1px solid #999999");
    $(".modify1").attr("readOnly", false);
    $(".modify1").css("border", "1px solid #999999");
    $(".select_styles1").removeAttr("disabled");
    $(".select_styles1").css("border", "1px solid #999999");
}

/**
 * 编辑客户详情
 */
ReservationsInfoDlg.supplement = function () {
    $(".modify1").attr("readOnly", false);
    $(".modify1").css("border", "1px solid #999999");
    $(".select_styles1").removeAttr("disabled");
    $(".select_styles1").css("border", "1px solid #999999");
}

ReservationsInfoDlg.getEditto = function () {
    $("#remarks").attr("readOnly", "readOnly");
    $("#content").attr("readOnly", "readOnly");
    $(".modify").attr("readOnly", "true");
    $(".modify").css("border", "none");
    $(".select_styles").attr("disabled", "disabled");
    $(".select_styles").css("border", "none");
    $(".modify1").attr("readOnly", "true");
    $(".modify1").css("border", "none");
    $(".select_styles1").attr("disabled", "disabled");
    $(".select_styles1").css("border", "none");
}

/**
 * 删除客户详情
 */
ReservationsInfoDlg.deleteReservations = function () {
    var r = confirm("是否删除该用户")
    if (r == true) {
        this.clearData();
        this.collectData();
        if (!this.validate()) {
            return;
        }
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/reservations/deletes", function (data) {
            Feng.success("删除成功!");
            window.parent.ReservationsRecordTable.table.refresh();
            ReservationsInfoDlg.close()
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.reservationsInfoData);
        ajax.start();
    }
    else {
    }
}

/**
 * 显示部门选择的树
 *
 * @returns
 */
ReservationsInfoDlg.showReservationsSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentReservationsMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
ReservationsInfoDlg.hideReservationsSelectTree = function () {
    $("#parentReservationsMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
ReservationsInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('customerPhone').set('customerSex').set('customerAge').set('economicAbility')
        .set('qqNumber').set('wechatNumber').set('occupation').set('archiving').set('filingTime').set('statuss').set('maritalStatus')
        .set('entryClerk').set('obstaclePoint').set('oneChannel').set('twoChannel').set('thrChannel').set('oneMedium').set('twoMedium').set('thrMedium').set('appointmentArrivalTime').set('appointmentPayService')
        .set('isLarge').set('content').set('reservationsNum').set('accessDisease').set('remarks').set('classification').set('status').set('outpatientName').set('phoneSharShip');
}

//状态下拉框change事件
ReservationsInfoDlg.statussChange = function () {
    //如果选择的下拉框的值是过期，就把日期选择不可编辑
    if($("#statuss").val() == '过期'){//过期状态不可编辑
        $('#appointmentTime').attr("disabled",true);
        var myDates = new Date();
        myDates.setDate(myDates.getDate());
        var months;
        var days;
        var hours = myDates.getHours();
        var minutes = myDates.getMinutes();
        var seconds = myDates.getSeconds();
        if ((myDates.getMonth() + 1) < 10) {
            months = "0" + (myDates.getMonth() + 1);
        } else {
            months = "" + (myDates.getMonth() + 1);
        }
        if (myDates.getDate() < 10) {
            days = "0" + myDates.getDate();
        } else {
            days = "" + myDates.getDate();
        }
        document.getElementById("appointmentTime").value = myDates.getFullYear() + "-" + months + "-" + days + " " + hours +":" + ":" + minutes + ":" + seconds;

    }else{//其他情况可编辑
        $('#appointmentTime').attr("disabled",false);
    }

}


ReservationsInfoDlg.getArchivings = function () {
    var index = layer.open({
        type: 2,
        title: '选中建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservation/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.getEditfs = function () {
    $(".modify2").attr("readOnly", false);
    $(".modify2").css("border", "1px solid #999999");
}

/**
 * 验证数据是否为空
 */
ReservationsInfoDlg.validate = function () {
    $('#reservationsInfoForm').data("bootstrapValidator").resetForm();
    $('#reservationsInfoForm').bootstrapValidator('validate');
    return $("#reservationsInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加预约客户
 */
ReservationsInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    var appointmentPayService = $('#appointmentPayService').val();
    var customerSex = $('input[name="sex"]:checked').val();
    var reservationsNum = $("#reservationsNum").val(); //预约号
    var customerName = $("#customerName").val(); //姓名
    var customerPhone = $("#customerPhone").val(); //电话
    var appointmentArrivalTime = $("#appointmentArrivalTime").val(); //预约时间
    if (!this.validate()) {
        return;
    }
    if (reservationsNum == null || reservationsNum == "") {
        alert("请输入预约号！");
        return;
    }
    if (customerName == null || customerName == "") {
        alert("请输入姓名！");
        return;
    }
    if (customerPhone == null || customerPhone == "") {
        alert("请输入电话！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    if (appointmentArrivalTime == null || appointmentArrivalTime == "") {
        alert("请选择预约时间！");
        return;
    }

    this.reservationsInfoData["customerSex"] = customerSex;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/add", function (data) {
        if (data == 1) {
            Feng.success("添加成功!");
            window.parent.ReservationsRecordTable.table.refresh();
            ReservationsInfoDlg.close()
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/**
 * 添加回访记录
 */
ReservationsInfoDlg.addReturn = function () {
    var id = $('#id').val();
    var index = layer.open({
        type: 2,
        title: '添加回访记录',
        area: ['60%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_addReturn/' + id
    });
    this.layerIndex = index;
}

/**
 * 提交添加预约客户
 */
ReservationsInfoDlg.addSubmits = function () {
    this.clearData();
    this.collectData();
    var appointmentPayService = $('#appointmentPayService').val();
    if (!this.validate()) {
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/add", function (data) {
        if (data == 1) {
            Feng.success("添加成功!");
            window.parent.ReservationsRecordTable.table.refresh();
            ReservationsInfoDlg.close()
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/**
 * 未到编辑提交修改
 */
ReservationsInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    var status = $('#status').val();
    var appointmentPayService = $('#appointmentPayService').val();
    if (!this.validate()) {
        return;
    }
    if (status == "到店") {
        alert("已到店，如需修改请联系管理员！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    var phone2=$('#customerPhone2').val();
    if(phone2==null || phone2==""){
        phone2="phone2";
    }
    var belonger1=$('#belonger1').val();
    var belonger2=$('#belonger2').val();
    alert(belonger1)
    alert(belonger2)
    var belonger=belonger2;
    if(belonger1==null||belonger1==""){
        belonger=belonger2;
    }else if(belonger2==null||belonger2==""){
        belonger=belonger1;
    }
    if(belonger==null||belonger==""){
        belonger=" ";
    }
    var cri=phone2+","+belonger;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/update/"+cri, function (data) {
        if (data == 1) {
            Feng.success("修改成功!");
            window.parent.ReservationsRecordTable.table.refresh();
            ReservationsInfoDlg.getEditto()
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/**
 * 补单提交修改
 */
ReservationsInfoDlg.supplementSubmit = function () {
    this.clearData();
    this.collectData();
    var status = $('#status').val();
    var appointmentPayService = $('#appointmentPayService').val();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/supplement_update", function (data) {
        if (data == 1) {
            Feng.success("修改成功!");
            window.parent.ReservationsRecordTable.table.refresh();
            ReservationsInfoDlg.getEditto()
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ReservationsInfoDlg.adminEditSubmit = function () {
    this.clearData();
    this.collectData();
    var status = $('#status').val();
    var appointmentPayService = $('#appointmentPayService').val();
    if (!this.validate()) {
        return;
    }
    if (status == "到店") {
        alert("已到店，如需修改请联系管理员！");
        return;
    }
    if (appointmentPayService == null || appointmentPayService == "") {
        alert("请选择预约项目！");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/adminUpdate", function (data) {
        if (data == 1) {
            Feng.success("修改成功!");
            window.parent.Reservations.table.refresh();
            ReservationsInfoDlg.close()
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

ReservationsInfoDlg.addChatRecord = function () {
    this.clearData();
    this.collectData();
    var status = $('#status').val();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/addChatRecord", function (data) {
        Feng.success("添加成功!");
        window.parent.ReservationsRecordTable.table.refresh();
        ReservationsInfoDlg.close()
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/*建档，跟进点击*/
ReservationsInfoDlg.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '添加建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

var seletprovince = $('#provinceValue').val();
var seletcities = $('#citiesValue').val();
var seletregion = $('#regionValue').val();
//初始化数据
ReservationsInfoDlg.initData = function () {
    //状态单选
    var seletecustomerSex = $('#customerSexValue').val();
    if ($('#id').val() != "") {
        $("#customerSex option[value=" + seletecustomerSex + "]").attr("selected", "selected");
    }
    var seletemaritalStatus = $('#maritalStatusValue').val();
    if ($('#id').val() != "") {
        $("#maritalStatus option[value=" + seletemaritalStatus + "]").attr("selected", "selected");
    }
    var seleteisLarge = $('#isLargeValue').val();
    if ($('#id').val() != "") {
        $("#isLarge option[value=" + seleteisLarge + "]").attr("selected", "selected");
    }
    var seletestatus = $('#statusValue').val();
    if ($('#id').val() != "") {
        $("#status option[value=" + seletestatus + "]").attr("selected", "selected");
    }
    var seletephoneSharShip = $('#phoneSharShipValue').val();
    if ($('#id').val() != "" && seletephoneSharShip != "") {
        $("#phoneSharShip option[value=" + seletephoneSharShip + "]").attr("selected", "selected");
    }
    var seleteaccessDisease = $('#accessDiseaseValue').val();
    if ($('#id').val() != "" && seleteaccessDisease != "") {
        $("#accessDisease option[value=" + seleteaccessDisease + "]").attr("selected", "selected");
    }
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentReservationsMenu" || $(
            event.target).parents("#parentReservationsMenu").length > 0)) {
        ReservationsInfoDlg.hideReservationsSelectTree();
    }
}

$(function () {
    Feng.initValidator("reservationsInfoForm", ReservationsInfoDlg.validateFields);
    ReservationsInfoDlg.initData();
    // $("#province option[value=" + seletprovince + "]").attr("selected", "selected");
    // $("#cities option[value=" + seletcities + "]").attr("selected", "selected");
    // $("#region option[value=" + seletregion + "]").attr("selected", "selected");
    //初始化下拉交通工具列表
    $.post(Feng.ctxPath + "/vehicle/getList", function (data) {
        var txt = '<option value="">--请选择交通工具--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].vehicle_name + '">' + data[i].vehicle_name + '</option>';
        }
        //状态单选
        var seletvehicle = $('#vehicleValue').val();
        if ($('#id').val() != "" && seletvehicle != "") {
            $("#vehicle option[value=" + seletvehicle + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉经济能力列表
    $.post(Feng.ctxPath + "/economicAbility/getList", function (data) {
        var txt = '<option value="">--请选择经济能力--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].economic_ability + '">' + data[i].economic_ability + '</option>';
        }
        document.getElementById('economicAbility').innerHTML = txt;
        //状态单选
        var seleteconomicAbility = $('#economicAbilityValue').val();
        if ($('#id').val() != "" && seleteconomicAbility != "") {
            $("#economicAbility option[value=" + seleteconomicAbility + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉经济能力列表
    $.post(Feng.ctxPath + "/occupation/getList", function (data) {
        var txt = '<option value="">--请选择职业--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].occupation + '">' + data[i].occupation + '</option>';
        }
        document.getElementById('occupation').innerHTML = txt;
        //状态单选
        var seleteoccupation = $('#occupationValue').val();
        if ($('#id').val() != "" && seleteoccupation != "") {
            $("#occupation option[value=" + seleteoccupation + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if ($('#id').val() != "" && seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/payService/getList", function (data) {
        var txt = '<option value="">--请选择预约项目--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].department + '">' + data[i].department + '</option>';
        }
        document.getElementById('appointmentPayService').innerHTML = txt;
        //状态单选
        var seletepayservicename = $('#payservicenameValue').val();
        if ($('#id').val() != "" && seletepayservicename != "") {
            $("#appointmentPayService option[value=" + seletepayservicename + "]").attr("selected", "selected");
        }
    }, "json");

    $(".modify").attr("readOnly","true");
    $(".select_styles").attr("disabled","disabled");
    $(".modify1").attr("readOnly","true");
    $(".select_styles1").attr("disabled","disabled");

});
